package com.proactive.webapp.controller;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.proactive.webapp.dao.CardTemplateDAO;
import com.proactive.webapp.dao.CourseDAO;
import com.proactive.webapp.dao.DesignDAO;
import com.proactive.webapp.dao.MemberDAO;
import com.proactive.webapp.dao.MenuDAO;
import com.proactive.webapp.dao.ResourceDAO;
import com.proactive.webapp.dao.WorksheetDAO;
import com.proactive.webapp.entity.CardTemplate;
import com.proactive.webapp.entity.Course;
import com.proactive.webapp.entity.Design;
import com.proactive.webapp.entity.Member;
import com.proactive.webapp.entity.Menu;
import com.proactive.webapp.entity.Resource;
import com.proactive.webapp.entity.Worksheet;
import com.proactive.webapp.util.MenuUtil;

@Controller
public class IndexController {
	ApplicationContext context =  new ClassPathXmlApplicationContext("spring-module.xml");
	
	@RequestMapping(value = {"/index", "/"}, method = RequestMethod.GET)
	public ModelAndView index(HttpServletRequest request, HttpSession session) {
		//check login
		Member loginMember = (Member)session.getAttribute("loginMember");
		ModelAndView model = new ModelAndView(loginMember==null?"index2":"index1");
		String loginAccount = loginMember==null?"":loginMember.getAccount();
		
		//import DAO
		CourseDAO courseDAO = (CourseDAO) context.getBean("courseDAO");
		DesignDAO designDAO = (DesignDAO) context.getBean("designDAO");
		WorksheetDAO worksheetDAO = (WorksheetDAO) context.getBean("worksheetDAO");
		ResourceDAO resourceDAO = (ResourceDAO) context.getBean("resourceDAO");
		CardTemplateDAO cardTemplateDAO = (CardTemplateDAO) context.getBean("cardTemplateDAO");
		MemberDAO memberDAO = (MemberDAO) context.getBean("memberDAO");
		
		List<Course> courseList = new ArrayList<Course>();
		List<Design> designList = new ArrayList<Design>();
		List<Worksheet> worksheetList = new ArrayList<Worksheet>();
		List<Resource> resourceList = new ArrayList<Resource>();
		List<CardTemplate> cardTemplateList = new ArrayList<CardTemplate>();	
		List<Member> memberList = new ArrayList<Member>();
		
		courseList = courseDAO.getListByType("new", 0, 8);
		designList = designDAO.getListByType("new", 0, 8);
		worksheetList = worksheetDAO.getListByType("new", 0, 6);
		resourceList = resourceDAO.getListByType(loginAccount, "new", 0, 8);
		cardTemplateList = cardTemplateDAO.getListByStatus(1, 1, 0, 8);
		memberList = memberDAO.getListTop(6);
		
		List<Menu> menuList = MenuUtil.getList(((MenuDAO)context.getBean("menuDAO")).getList(), 0);
		
		//set model
		model.addObject("courseList", courseList);
		model.addObject("designList", designList);
		model.addObject("worksheetList", worksheetList);
		model.addObject("resourceList", resourceList);
		model.addObject("cardTemplateList", cardTemplateList);
		model.addObject("memberList", memberList);
		model.addObject("menuList", menuList);
		

		return model;
	}

}
